//
//  ViewController.m
//  MY QQapp
//
//  Created by liuchengming on 16/10/31.
//  Copyright © 2016年 liuchengming. All rights reserved.
//

#import "ViewController.h"
#import "ContactTableViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *UesrName;
@property (weak, nonatomic) IBOutlet UITextField *PassWord;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //self.navigationController.hidesBarsWhenKeyboardAppears = NO;
    UIToolbar * toolbar  = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    UIBarButtonItem * item = [[UIBarButtonItem alloc]initWithTitle:@"收起键盘" style:UIBarButtonItemStylePlain target:self action:@selector(endEdit)];
    UIBarButtonItem * item2 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];
    UIBarButtonItem * space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
    self.UesrName.inputAccessoryView = toolbar;
    toolbar.items = @[item2,space,item];
    //[self.view addSubview:toolbar];
    
    
}
//收回键盘／结束编辑
-(void) endEdit
{
    [self.view endEditing:YES];
}
// 无法登陆
- (IBAction)noLoing:(id)sender
{
    UIAlertController * aler = [UIAlertController alertControllerWithTitle:@"无法登陆" message:@"其实你点了也没什么卵用～" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action)
    {
        
    }];
    [aler addAction:action];
    
   [self presentViewController:aler animated:YES completion:^{}];
}

//网络连接
- (IBAction)onClick:(id)sender
{
    
    NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"http://xl18z.cn/login.php?name=%@&password=%@",_UesrName.text,_PassWord.text]];
    NSURLRequest * req = [NSURLRequest requestWithURL:url];
    NSURLSession * session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask * task = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
    {
        //NSString * text = [[NSString alloc]initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
        //NSLog(@"%@",text);
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        //大中枢派发
        dispatch_async(dispatch_get_main_queue(), ^
        {
            if ([dict[@"errcode"]intValue] == 0)
            {
                ContactTableViewController * con = [self.storyboard instantiateViewControllerWithIdentifier:@"tabbar"];
                //以modal方式推入页面
                [self presentViewController:con animated:YES completion:^
                {
                    
                }];
            }else
            {
                NSString * str = [dict objectForKey:@"errinfo"];
                UIAlertController * aler = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:aler animated:YES completion:^
                {
                }];
                UIAlertAction * action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull  action)
                {
                }];
                [aler addAction:action];
            }
        });
    }];
    [task resume];
}


//点击空白收回键盘

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
